#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

namespace TL
{
	template<class K>
	struct BSTNode
	{
		K _key;
		BSTNode<K>* _left;
		BSTNode<K>* _right;

		BSTNode(const K& key = K())
			:_key(key)
			,_left(nullptr)
			,_right(nullptr)
		{}
	};

	template<class K>
	class BSTree
	{
		typedef BSTNode<K> Node;
	public:
		BSTree(const initializer_list<K>& il)
		{
			for (auto& e : il)
			{
				insert(e);
			}
		}

		BSTree()
			:_root(nullptr)
		{}

		bool insert(const K& key)
		{
			Node* newnode = new Node(key);
			if (_root == nullptr)
			{
				_root = newnode;
			}
			else
			{
				Node* pcur = _root;
				Node* parent = _root;
				while (pcur)
				{
					if (key < pcur->_key)
					{
						parent = pcur;
						pcur = pcur->_left;
					}
					else if(key > pcur->_key)
					{		
						parent = pcur;
						pcur = pcur->_right;
					}
					else
					{
						return false;
					}
				}
				if (key < parent->_key)
				{
					parent->_left = newnode;
				}
				else
				{
					parent->_right = newnode;
				}
			}
			return true;
		}

		bool find(const K& key)
		{
			Node* pcur = _root;
			while (pcur)
			{
				if (key < pcur->_key)
				{
					pcur = pcur->_left;
				}
				else if (key > pcur->_key)
				{
					pcur = pcur->_right;	
				}
				else
				{
					return true;
				}
			}
			return false;
		}

		bool erase(const K& key)
		{
			Node* pcur = _root;
			Node* parent = _root;
			while (pcur)
			{
				if (key < pcur->_key)
				{
					parent = pcur;
					pcur = pcur->_left;
				}
				else if (key > pcur->_key)
				{
					parent = pcur;
					pcur = pcur->_right;
				}
				else
				{
					if (pcur->_left == nullptr)
					{
						if (pcur == _root)
						{
							_root = pcur->_right;
						}
						else 
						{
							if (parent->_left == pcur)
							{
								parent->_left = pcur->_right;
							}
							else
							{
								parent->_right = pcur->_right;
							}
						}
						delete pcur;
					}
					else if (pcur->_right == nullptr)
					{
						if (pcur == _root)
						{
							_root = pcur->_left;
						}
						if (parent->_left == pcur)
						{
							parent->_left = pcur->_left;
						}
						else
						{
							parent->_right = pcur->_left;
						}
						delete pcur;
					}
					else
					{
						Node* Minright = pcur->_right;
						Node* MinrightParent = pcur;
						while (Minright->_left)
						{
							MinrightParent = Minright;
							Minright = Minright->_left;
						}
						pcur->_key = Minright->_key;
						if (Minright == MinrightParent->_left)
						{
							MinrightParent->_left = Minright->_right;
						}
						else
						{
							MinrightParent->_right = Minright->_right;
						}
						delete Minright;
					}
					return true;
				}
			}
			return false;
		}

		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}

	private:
		Node* _root = nullptr;

		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}
			_InOrder(root->_left);
			cout << root->_key << " ";
			_InOrder(root->_right);
		}
	};
}